package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.QiYeZhongXinActivity;
import com.tangkun.environment.entity.shouye.ErrorListReturn;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * @author tangkun
 * @ClassName: XiTongTongZhiAdapter
 * @Description: 系统通知列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class TiJianAdapter extends BaseAdapter {

    private Context context;
    private List<ErrorListReturn.DataBean> dataList = new ArrayList<ErrorListReturn.DataBean>();
    /**
     * type 0:基础数据 1：巡查数据
     */
    private int type = 0;

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public TiJianAdapter(Context context, List<ErrorListReturn.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_tijian, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (dataList != null) {
            //是否有异常
            if (dataList.get(position).errornum > 0) {
                holder.ivTijianYichang.setVisibility(View.VISIBLE);
            } else {
                holder.ivTijianYichang.setVisibility(View.INVISIBLE);
            }
            //头像
            Glide.with(context)
                    .load(GlobalUrl.IMG_URL + dataList.get(position).img)
                    .bitmapTransform(new CropCircleTransformation(context))
                    .placeholder(R.mipmap.ic_guanlianqiye_shangchuandianzhao)
                    .into(holder.civTijianPhoto);
            //店铺名字
            if (!TextUtils.isEmpty(dataList.get(position).name)) {
                holder.tvTijianName.setText(dataList.get(position).name);
            } else {
                holder.tvTijianName.setText("");
            }
            //状态
            switch (type) {
                case 0:
                    if (dataList.get(position).errornum > 0) {
                        holder.tvTijianZhuangtai.setText(dataList.get(position).errornum + "组基础数据异常");
                        holder.tvTijianZhuangtai.setTextColor(context.getResources().getColor(R.color.common_red));
                    } else {
                        holder.tvTijianZhuangtai.setText("基础数据正常");
                        holder.tvTijianZhuangtai.setTextColor(context.getResources().getColor(R.color.common_tips));
                    }
                    break;
                case 1:
                    if (dataList.get(position).errornum > 0) {
                        holder.tvTijianZhuangtai.setText(dataList.get(position).errornum + "组巡查数据异常");
                        holder.tvTijianZhuangtai.setTextColor(context.getResources().getColor(R.color.common_red));
                    } else {
                        holder.tvTijianZhuangtai.setText("巡查数据正常");
                        holder.tvTijianZhuangtai.setTextColor(context.getResources().getColor(R.color.common_tips));
                    }
                    break;
            }

            //跳转企业中心
            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), QiYeZhongXinActivity.class);
                    i.putExtra("id", dataList.get(position).ids);//企业ID
                    i.putExtra("name", dataList.get(position).name);
                    v.getContext().startActivity(i);
                }
            });
        }
        return convertView;

    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.civ_tijian_photo)
        CircleImageView civTijianPhoto;
        @BindView(R.id.iv_tijian_yichang)
        ImageView ivTijianYichang;
        @BindView(R.id.tv_tijian_name)
        TextView tvTijianName;
        @BindView(R.id.tv_tijian_zhuangtai)
        TextView tvTijianZhuangtai;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
